package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.PersonalMessage;
import cn.wolfcode.trip.base.query.PersonalMessageQueryObject;
import cn.wolfcode.trip.base.query.SystemMessagesQueryObject;
import cn.wolfcode.trip.base.service.IPersonalMessageService;
import cn.wolfcode.trip.base.service.ISystemMessagesService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/personalmessages")
public class PersonalMessageController {

    @Autowired
    private IPersonalMessageService personalMessageService;

    //系统列表
    @GetMapping("/{userId}")
    @ResponseBody
    public Object list(@ModelAttribute("qo")PersonalMessageQueryObject qo){
        qo.setOrderBy("pm.createTime desc");
        return  personalMessageService.queryForSenderByGetterId(qo);

    }
    @GetMapping("/{userId}/{senderId}/chatRecords")
    @ResponseBody
    public Object chatRecords(@ModelAttribute("qo")PersonalMessageQueryObject qo){
        qo.setOrderBy("pm.createTime desc");
        return  personalMessageService.queryForChatRecords(qo);

    }
    @PostMapping("/{userId}/{senderId}/send")
    @ResponseBody
    public Object send(PersonalMessage personalMessage){
        JSONResult result = new JSONResult();

        try {
            personalMessageService.save(personalMessage);
            PersonalMessage pm = personalMessageService.selectByPrimaryKey(personalMessage.getId());
            personalMessage.setSender(pm.getSender());

            result.setObj(personalMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
    @PutMapping("/{userId}/{senderId}/updateState")
    @ResponseBody
    public Object updateState(PersonalMessage personalMessage){
        JSONResult result = new JSONResult();

        try {
            personalMessageService.updateState(personalMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
